package face.pay.job.config;

import com.aliyun.oss.sdk.OSSUtils;
import com.face.pay.AliPayConfig;
import com.face.pay.IPayConfigServer;
import com.face.pay.SecurityConfig;
import com.face.pay.WxApiConfig;
import com.google.common.cache.Cache;
import face.pay.cache.LocalCache;
import face.pay.dao.FileInfoDao;
import face.pay.dao.OemConfigInfoDao;
import face.pay.dao.OemInfoDao;
import face.pay.entity.FileInfo;
import face.pay.entity.OemConfigInfo;
import face.pay.entity.OemInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Auther: zkj
 * @Date: 2019/8/26 16:11
 * @Description:
 */
@Service
public class ApiConfigServer implements IPayConfigServer {

    @Autowired
    private OemInfoDao oemInfoDao;

    @Autowired
    private OemConfigInfoDao oemConfigInfoDao;

    @Autowired
    private FileInfoDao fileInfoDao;


    private Cache cache = LocalCache.cache;

    @Transactional
    public WxApiConfig getConfig(Integer oemId){

        OemInfo oemInfo = this.oemInfoDao.getOemInfoById(oemId);

        Long piclientCertp12 =oemInfo.getApiclientCertp12();


        FileInfo crtp12fileInfo = this.fileInfoDao.getById(piclientCertp12);

        WxApiConfig wxApiConfig =  new WxApiConfig();


        if(crtp12fileInfo!=null){

            OemConfigInfo oemConfigInfo =  this.oemConfigInfoDao.getByOemId(oemId);
            if(oemConfigInfo!=null){

                SecurityConfig securityConfig =  new SecurityConfig(
                        oemId,
                        oemConfigInfo.getAccessKeyId(),
                        oemConfigInfo.getAccessKeySecret(),
                        oemConfigInfo.getBucket(),
                        oemConfigInfo.getEndPoint(),
                        oemInfo.getAppId(),
                        oemInfo.getApiKey(),
                        oemInfo.getMchId(),
                        crtp12fileInfo.getFullName(),
                        oemInfo.getApi3Key(),
                        oemConfigInfo.getFaceKey()
                        );
                wxApiConfig.setSecurityConfig(securityConfig);
                wxApiConfig.setOemType(oemInfo.getType());
            }

        }

        return  wxApiConfig;

    }

    @Override
    public AliPayConfig getAliConfig(Integer oemId) {


        OemInfo oemInfo = this.oemInfoDao.getOemInfoById(oemId);

        Long aliAppKey =oemInfo.getAliAppKey();

        Long aliPublicKey =oemInfo.getAliPublicKey();

        FileInfo aliAppKeyFile = this.fileInfoDao.getById(aliAppKey);
        FileInfo aliPublicKeyFile = this.fileInfoDao.getById(aliPublicKey);

        if(aliAppKeyFile!=null && aliPublicKeyFile!=null){
            OemConfigInfo oemConfigInfo =  this.oemConfigInfoDao.getByOemId(oemId);
            String appkey  ="";
            String publicKey  ="";

            try{
               Object appkeyObj =   cache.getIfPresent(aliAppKey);
                if(appkeyObj==null){
                    byte[] appkeyBytes = OSSUtils.getOSSBytes(oemConfigInfo.getEndPoint(),
                            oemConfigInfo.getAccessKeyId(),
                            oemConfigInfo.getAccessKeySecret(),
                            oemConfigInfo.getBucket(),
                            aliAppKeyFile.getFullName()
                    );
                    if(appkeyBytes!=null){
                        /**
                         * 可以做缓存
                         */
                        appkey  = new String(appkeyBytes,"GBK");
                        cache.put(aliAppKey,appkey);
                    }
                }else {
                    appkey =  appkeyObj.toString();
                }

                Object publicKeyObj =   cache.getIfPresent(aliPublicKey);
                if(publicKeyObj==null){

                    byte[] aliPublicKeyBytes = OSSUtils.getOSSBytes(oemConfigInfo.getEndPoint(),
                            oemConfigInfo.getAccessKeyId(),
                            oemConfigInfo.getAccessKeySecret(),
                            oemConfigInfo.getBucket(),
                            aliPublicKeyFile.getFullName()
                    );
                    if(aliPublicKeyBytes!=null){
                        /**
                         * 可以做缓存
                         */
                        publicKey  = new String(aliPublicKeyBytes,"GBK");
                        cache.put(aliPublicKey,publicKey);
                    }
                }else {
                    publicKey = publicKeyObj.toString();
                }
                AliPayConfig aliPayConfig = new AliPayConfig(oemId,oemInfo.getAliAppId(),appkey);
                aliPayConfig.setAliPublicKey(publicKey);
                aliPayConfig.setAliProviderId(oemInfo.getAliProviderId());
                aliPayConfig.setOemType(oemInfo.getType());
                return  aliPayConfig;

            }catch (Exception e){
                //log.error("获取alipauy配置异常",e);
            }
        }
        return null;
    }
}
